package com.epoch.client.kernel.listener;

import com.epoch.client.kernel.serve.ClientConnService;
import com.epoch.client.view.ClientLoginJFrame;
import com.epoch.common.UserRegister;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@Slf4j
public class RegisterListener implements ActionListener {
    JTextField name = null;
    JPasswordField Password1,Password2 = null;
    JFrame jf;
    UserRegister register;

    public RegisterListener(JTextField name, JPasswordField password1, JPasswordField password2, JFrame jf) {
        this.name = name;
        Password1 = password1;
        Password2 = password2;
        this.jf = jf;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        //获取用户注册内容
        String username = name.getText().trim();
        String password = new String(Password1.getPassword());
        String passwordPre = new String(Password2.getPassword());

        //将信息封装到UserRes类中
        register = new UserRegister(username,password,passwordPre);
        log.info("用户名"+username+",密码"+password+",二次验证密码"+passwordPre);

        //判断用户信息的合法性
        if (username.length()==0 || password.length()==0 || passwordPre.length()==0) {
            JOptionPane.showMessageDialog(null,"信息不完整！！！");
        } else if(!password.equals(passwordPre)) {
            JOptionPane.showMessageDialog(null,"密码不一致！！！");
        } else {
            boolean check = new ClientConnService().checkUserSignup(register);
            //获取服务器回送的用户注册相关信息
            if (check) {
                JOptionPane.showMessageDialog(null,"恭喜您！注册成功！");
                new ClientLoginJFrame();//回到登录界面
                jf.dispose();
            } else {
                JOptionPane.showMessageDialog(null,"注册失败！请重试！");
            }
        }
    }

}
